package com.ybkj.daijia.server.event.listener.zhuan;

import com.alibaba.fastjson.JSONObject;
import com.ybkj.daijia.PushData;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.server.event.model.zhuan.ZhuanOrderFillEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverConsumedRecordService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.FinancialRecordService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.zhuan.Car;
import com.ybkj.daijia.server.zhuan.Order;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ZhuanOrderFillEventListener implements ApplicationListener<ZhuanOrderFillEvent> {

    private static final Logger logger = LoggerFactory.getLogger(ZhuanOrderFillEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private DriverConsumedRecordService driverConsumedRecordService;

    @Autowired
    private FinancialRecordService financialRecordService;

    @Autowired
    private ZhuancheService zhuancheService;

//	@Autowired
//	private SuperviceService superviceService;


    @Override
    @Async
    public void onApplicationEvent(ZhuanOrderFillEvent event) {

        logger.debug("order Accept event:{}", "");
        Long orderId = (Long) event.getSource();
        Long employId = event.getEmployId();

        Order order = zhuancheService.findOne(orderId);

        Driver driver = driverService.findOne(employId);

        driverService.updateStatusAndJobStatus(Driver.DRIVER_BUSY, Driver.DRIVER_JOBSTATUS_ZHUANCHE,
            driver.getId());

        Company orderCompany = companyService.findOne(order.getCompanyId());

        Company finalCompany = orderCompany;
        Company main = companyService.getMain();
        Setting setting = settingUtils.get();
        if (!main.getId().equals(orderCompany.getId()) && setting.isSmsMainCompany()) {
            finalCompany = main;
        }

        zhuancheService.acceptInfoFee(driver, order);
        Car car = zhuancheService.findCar(driver.getCarId());

        Passenger passenger = memberService.findOnePassenger(order.getPassengerId());
        //给客户推送
        passenger.setCid(memberService.findCid(passenger.getId()));
        if (StringUtils.isNotBlank(passenger.getCid())) {
            try {
                //发送设备推送
                PushData pushData = new PushData();
                pushData.setType("ORDER_ACCEPT_ZHUANCHE");
                pushData.setTitle("新提醒");
                pushData.setDescription("服务人员已接受您的订单");
                pushData.setData(order.getId());

                GeTuiMember.push(passenger, pushData, settingUtils.get());
            } catch (IOException e) {
                logger.debug("push error : " + e.getMessage());
            }
        }

        //发送短信
        try {
            SmsTemplet templet = smsService
                .findByAlias(finalCompany.getId(), SmsType.acceptZhuanxianOrder);
            if (null != templet && StringUtils.isNotBlank(templet.getContent())) {
                String carNumber = car != null ? car.getCarNumber() : "";
                //smsService.sendSms(order.getPassengerPhone(), "acceptZhuancheOrder","{\"服务人员名\":\""+driver.getName()+"\",\"服务人员工号\":\""+driver.getUsername()+"\",\"服务人员电话\":\""+driver.getPhone()+"\",\"车牌号\":\""+carNumber+"\",\"订单号\":\""+"***"+order.getOrderNo().substring(8)+"\"}", finalCompany.getId(), 0, finalCompany.getSignName());
                //短信发送改
                JSONObject smsParam = new JSONObject();
                smsParam.put("name", driver.getName());
                smsParam.put("carNumber", carNumber);
                smsParam.put("tel", driver.getPhone());
                smsService.sendSms(order.getPassengerPhone(), templet.getTemplateId(),
                    smsParam.toJSONString(), finalCompany.getId(), 0, finalCompany.getSignName());
            }
        } catch (Exception e) {
            logger.error("发送短信发生异常：{}", e.getMessage());
        }

        //推送司机补单至监管平台
//		superviceService.pushDdjs(order);           //下单
//		superviceService.pushDdpd(order, driver);   //派单
    }

}
